<?php
/**
 * 订单处理
 *
 * @category 
 * @package 
 * @author chenping<chenping@shopex.cn>
 * @version $Id: Z
 */
class erpapi_shop_request_order extends erpapi_shop_request_abstract
{
    /**
     * 淘宝全链路
     *
     * @return void
     * @author 
     **/
    public function message_produce($sdf,$queue=false){}
    
    /**
     * 获取店铺订单详情
     *
     * @param String $order_bn 订单号
     * @return void
     * @author
     **/
    public function get_order_detial($order_bn)
    {
        $params['tid'] = $order_bn;

        $title = "店铺(".$this->__channelObj->channel['name'].")获取前端店铺".$order_bn."的订单详情";

        $order_type = ($this->__channelObj->channel['business_type'] == 'zx') ? 'direct' : 'agent';

        $api_name = $order_type == 'direct' ? SHOP_TRADE_FULLINFO_RPC : SHOP_FENXIAO_TRADE_FULLINFO_RPC;

        $rsp = $this->__caller->call($api_name,$params,array(),$title,10,$order_bn);

        $result = array();
        $result['rsp']        = $rsp['rsp'];
        $result['err_msg']    = $rsp['err_msg'];
        $result['msg_id']     = $rsp['msg_id'];
        $result['res']        = $rsp['res'];
        $result['data']       = json_decode($rsp['data'],1);
        $result['order_type'] = $order_type;
        
        return $result;
    }

    #获取订单状态
    public function getOrderStatus($arrOrderBn)
    {
        $order_bn = implode(',', $arrOrderBn);
        $params = array('tids' => $order_bn);
        $title = "店铺(" . $this->__channelObj->channel['name'] . ")获取前端店铺" . $order_bn . "的订单详情";
        $rsp = $this->__caller->call(SHOP_GET_ORDER_STATUS, $params, array(), $title, 10, $order_bn);
        return $this->doGetOrderStatusRet($rsp);
    }
    
    protected function doGetOrderStatusRet($rsp) {
        $rsp['data'] = json_decode($rsp['data'], 1);
        return $rsp;
    }
    
    #订单编辑
    public function updateIframe($order,$is_request=true,$ext=array()) {
        // 默认本地编辑
        $data = array('edit_type'=>'local');
        return array('rsp'=>'success','msg'=>'本地订单编辑','data'=>$data);
    }

    #订单更新
    public function updateOrder($order){}

    public function updateOrderStatus($order , $status='' , $memo='' , $mode='sync'){}

    public function updateOrderTax($order){}

    public function updateOrderShipStatus($order,$queue = false) {}

    public function updateOrderPayStatus($order){}

    public function updateOrderMemo($order,$memo){}

    public function addOrderMemo($order,$memo){}

    public function addOrderCustomMark($order,$memo){}

    protected function __formatUpdateOrderShippingInfo($order) {
        return array();
    }

    public function updateOrderShippingInfo($order)
    {
        $rs = array('rsp'=>'fail','msg'=>'','data'=>'');
        if (!$order) {
            $rs['msg'] = 'no order';
            return $rs;
        }
        $params = $this->__formatUpdateOrderShippingInfo($order);
        if(empty($params)) {
            $rs['msg'] = 'no params';
            return $rs;
        }
        $callback = array(
            'class' => get_class($this),
            'method' => 'callback',
        );
        $title = '店铺('.$this->__channelObj->channel['name'].')更新[交易收货人信息]:'.$params['receiver_name'].'(订单号:'.$order['order_bn'].')';
        $rs = $this->__caller->call(SHOP_UPDATE_TRADE_SHIPPING_ADDRESS_RPC,$params,$callback,$title,10,$order['order_bn']);
        return $rs;
    }

    public function updateOrderConsignerinfo($order){}

    public function updateOrderSellagentinfo($order){}

    public function updateOrderLimitTime($order,$order_limit_time){}

    #获取店铺指定时间范围内的订单
    public function getOrderList($start_time,$end_time) {
        $rs = array('rsp'=>'fail','msg'=>'','data'=>array(),'is_update_time'=>'false');
        $orderModel = app::get('ome')->model('orders');
        $params = array();
        $params['start_time'] = date("Y-m-d H:m:s",$start_time);
        $params['end_time']   = date("Y-m-d H:m:s",$end_time);
        $params['page_size']  = 100;
        $params['fields']     = 'tid,status,pay_status,ship_status,modified';
        $primaryBn = $this->__channelObj->channel['shop_id'];
        $title = '获取店铺' . $this->__channelObj->channel['name'] . '(' . $params['start_time'] . '-' . $params['end_time'] . '时间内)的订单';
        $return_data = $this->__caller->call(SHOP_GET_TRADES_SOLD_RPC,$params,array(),$title,10,$primaryBn);
        if($return_data['data']) {
            $return_data['data'] = json_decode($return_data['data'], true);
        }
        if($return_data['rsp'] == 'succ')
        {
            if(intval($return_data['data']['total_results'])<1){
                $rs['msg']            = '该时间段内没有订单.';
                $rs['is_update_time'] = 'true';
                $rs['msg_id']         = $return_data['msg_id'];
                $rs['rsp']            = 'success';
                $rs['data']           = array();
                return $rs;
            }
            $page_total = ceil($return_data['data']['total_results']/$params['page_size']);
            $aTmp = array();
            for($i=1;$i<=$page_total;$i++)
            {
                $matrix_tids              = array();
                $params['page_no']        = $i;
                $return_data_page = $this->__caller->call(SHOP_GET_TRADES_SOLD_RPC,$params,array(),$title,10,$primaryBn);
                if($return_data_page['rsp'] == 'succ')
                {
                    foreach($return_data_page['data']['trades'] as $k=>$v){
                        $matrix_tids[$v['tid']]['status']      = $v['status'];
                        $matrix_tids[$v['tid']]['tid']         = $v['tid'];
                        $matrix_tids[$v['tid']]['modified']    = $v['modified'];
                        $matrix_tids[$v['tid']]['ship_status'] = $v['ship_status'];
                        $matrix_tids[$v['tid']]['pay_status']  = $v['pay_status'];
                    }//获取到矩阵返回的数据后，对数据进行重组
                    $matrix_tid_keys = array_keys($matrix_tids);
                    $row = $orderModel->getList('outer_lastmodify,order_bn',array('order_bn'=>$matrix_tid_keys));
                    if(empty($row)){
                        $aTmp = array_merge($matrix_tids,$aTmp);
                    }else{
                        $local_exist_tids = array();
                        foreach($row as $return_k=>$return_v)
                        {
                            if($row && !(strtotime($matrix_tids[$return_v['order_bn']]['modified'])>$return_v['outer_lastmodify'])){
                                $local_exist_tids[] = $return_v['order_bn'];//将本地不需要的订单放入数组
                            }
                        }
                        foreach ($local_exist_tids as $value) {
                            unset($matrix_tids[$value]);
                        }//将不需要修改的订单从总list中删除.
                        $aTmp = array_merge($matrix_tids,$aTmp);
                    }
                }
                else{
                    $rs['msg']    = $return_data_page['err_msg'];
                    $rs['msg_id'] = $return_data_page['msg_id'];
                    return $rs;
                };
            }
            if(count($aTmp)==0){
                $rs['msg']            = '经过比对,该时间段内没有发现漏单情况';
                $rs['is_update_time'] = 'true';
            }
        }else{
            $rs['msg']    = $return_data['err_msg'];
            $rs['msg_id'] = $return_data['msg_id'];
            return $rs;
        }
        $rs['data']   = $aTmp;
        $rs['rsp']    = 'success';
        $rs['msg_id'] = $return_data_page['msg_id'];
        return $rs;
    }

    public function cleanStockFreeze($order){}
}
